package cn.antho.pet.service.impl;

import cn.antho.basic.util.BaiduAuditUtils;
import cn.antho.basic.util.FreemarkHtml;
import cn.antho.basic.util.LoginContext;
import cn.antho.org.domain.Employee;
import cn.antho.org.service.IEmployeeService;
import cn.antho.org.service.IShopService;
import cn.antho.pet.domain.Pet;
import cn.antho.pet.domain.PetOnsaleAuditLog;
import cn.antho.pet.mapper.PetMapper;
import cn.antho.pet.mapper.PetOnsaleAuditLogMapper;
import cn.antho.pet.service.IPetDetailService;
import cn.antho.pet.service.IPetService;
import cn.antho.basic.service.impl.BaseServiceImpl;
import cn.antho.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author antho
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private IPetDetailService petDetailService;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnsaleAuditLogMapper petOnsaleAuditLogMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Value("${pagedir}")
    private String path;

    @Override
    public void add(Pet pet, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        pet.setShopId(employee.getShopId());
        super.add(pet);
        pet.getDetail().setPetId(pet.getId());
        petDetailService.add(pet.getDetail());
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        pet.getDetail().setPetId(pet.getId());
        petDetailService.update(pet.getDetail());
    }

    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) throws Exception {
        // 过滤到上架状态的数据 把数据转换成map key是主键 value是对象
        Map<Long, Pet> map = petMapper.findAll().stream().filter(pet -> pet.getState() == 0).collect(Collectors.toMap(Pet::getId, pet -> pet));
        Date date = new Date();
        String petPath = path + "\\petDetail";
        for (Long id : ids) {
            Pet pet = map.get(id);
            StringBuilder sb = new StringBuilder();
            // 不等于null 即数据本身是下架的
            if (null != pet) {
                if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                    sb.append("宠物上架百度AI审核名称失败！宠物名称为：" + pet.getName() + ";");
                }
                if (pet.getResources().split(",").length > 1) {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])) {
                        sb.append("宠物上架百度AI审核图片失败！图片地址为：" + pet.getResources() + ";");
                    }
                } else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())) {
                        sb.append("宠物上架百度AI审核图片失败！图片地址为：" + pet.getResources() + ";");
                    }
                }
                if (sb.length() > 0) {
                    PetOnsaleAuditLog petOnsaleAuditLog = new PetOnsaleAuditLog();
                    petOnsaleAuditLog.setAuditTime(date);
//                    petOnsaleAuditLog.setAuditId(employeeMapper.findByLogininfoId(LoginContext.getLogininfo(request).getId()).getId());
                    petOnsaleAuditLog.setPetId(id);
                    petOnsaleAuditLog.setNote(sb.toString());
                    petOnsaleAuditLog.setState(0);
                    petOnsaleAuditLogMapper.add(petOnsaleAuditLog);
                    continue;
                }
                pet.setState(1L);
                pet.setOnsaletime(date);
                petMapper.update(pet);
                Pet petDetail = petMapper.getPetDetail(id);
                FreemarkHtml.staticHtml(petPath, petDetail, petDetail.getId());
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        ids.forEach(id->{
            String str = path + "\\"+ id + ".html";
            File file = new File(str);
            file.delete();
        });
        // 全部查询出来 只要状态是下架就不处理
        petMapper.batchOffsale(ids);

    }

    /**
     * 根据宠物id查询宠物详情
     * @param id
     * @return
     */
    @Override
    public Pet getPetDetail(Long id) {
        return petMapper.getPetDetail(id);
    }

}
